package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.EventLogMapper;
import cc.rengu.igas.bsps.common.entity.EventLog;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 事件操作日志表操作接口实现类
 * Created by 王成 on 2018/10/26.
 */
public class EventLogMapperImpl implements EventLogMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public int insertEventLogInfo(EventLog eventLog) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertEventLogInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, eventLog, null);
        if (0 != iReturnCode) {
            rglog.error("登记事件申报日志信息失败，iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public List<EventLog> selectEventLogInfoByEventId(String instId, String eventId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectEventLogInfoByEventId");
        EventLog eventLog = new EventLog();
        eventLog.setInstId(instId);
        eventLog.setEventId(eventId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, eventLog, EventLog.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构标识<{}>及事件标识<{}>获取全量事件操作日志信息失败!", instId, eventId);
            return null;
        }
        return objectList.stream().map(item -> (EventLog) (item)).collect(Collectors.toList());
    }
}
